home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Leser 15 / Amiga Plus Leser CD 15.iso / Tools / Development / MosaicSRC / libwww2 / Unused / HTPasswd.c < prev   
Encoding:
C/C++ Source or Header  |  2002-03-13  |  6.5 KB  |  265 lines

  1.  
  2. /* MODULE                            HTPasswd.c
  3. **        PASSWORD FILE ROUTINES
  4. **
  5. ** AUTHORS:
  6. **    AL    Ari Luotonen    luotonen@dxcern.cern.ch
  7. **
  8. ** HISTORY:
  9. **
  10. **
  11. ** BUGS:
  12. **
  13. **
  14. */
  15.  
  16.  
  17. #include <string.h>
  18.  
  19. #include "HTUtils.h"
  20. #include "HTAAUtil.h"    /* Common parts of AA    */
  21. #include "HTAAFile.h"    /* File routines    */
  22. #include "HTPasswd.h"    /* Implemented here    */
  23. #include "tcp.h"    /* FROMASCII()        */
  24.  
  25. extern char *crypt();
  26.  
  27.  
  28. PRIVATE char salt_chars [65] =
  29.     "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
  30.  
  31. /* PUBLIC                        HTAA_encryptPasswd()
  32. **        ENCRYPT PASSWORD TO THE FORM THAT IT IS SAVED
  33. **        IN THE PASSWORD FILE.
  34. ** ON ENTRY:
  35. **    password    is a string of arbitrary lenght.
  36. **
  37. ** ON EXIT:
  38. **    returns        password in one-way encrypted form.
  39. **
  40. ** NOTE:
  41. **    Uses currently the C library function crypt(), which
  42. **    only accepts at most 8 characters long strings and produces
  43. **    always 13 characters long strings. This function is
  44. **    called repeatedly so that longer strings can be encrypted.
  45. **    This is of course not as safe as encrypting the entire
  46. **    string at once, but then again, we are not that paranoid
  47. **    about the security inside the machine.
  48. **
  49. */
  50. PUBLIC char *HTAA_encryptPasswd ARGS1(CONST char *, password)
  51. {
  52.     char salt[3];
  53.     char chunk[9];
  54.     char *result;
  55.     char *tmp;
  56.     CONST char *cur = password;
  57.     int len = strlen(password);
  58.     extern time_t theTime;
  59.     int random = (int)theTime;    /* This is random enough */
  60.  
  61.     if (!(result = (char*)malloc(13*((strlen(password)+7)/8) + 1)))
  62.     outofmem(__FILE__, "HTAA_encryptPasswd");
  63.  
  64.     *result = (char)0;
  65.     while (len > 0) {
  66.     salt[0] = salt_chars[random%64];
  67.     salt[1] = salt_chars[(random/64)%64];
  68.     salt[2] = (char)0;
  69.  
  70.     strncpy(chunk, cur, 8);
  71.     chunk[8] = (char)0;
  72.  
  73.     tmp = crypt((char*)password, salt);  /*crypt() doesn't change its args*/
  74.     strcat(result, tmp);
  75.     free(tmp);
  76.  
  77.     cur += 8;
  78.     len -= 8;
  79.     } /* while */
  80.  
  81.     return result;
  82. }
  83.  
  84.  
  85.  
  86. /* PUBLIC                        HTAA_passwdMatch()
  87. **        VERIFY THE CORRECTNESS OF A GIVEN PASSWORD
  88. **        AGAINST A ONE-WAY ENCRYPTED FORM OF PASSWORD.
  89. ** ON ENTRY:
  90. **    password    is cleartext password.
  91. **    encrypted    is one-way encrypted password, as returned
  92. **            by function HTAA_encryptPasswd().
  93. **            This is typically read from the password
  94. **            file.
  95. **
  96. ** ON EXIT:
  97. **    returns        YES, if password matches the encrypted one.
  98. **            NO, if not, or if either parameter is NULL.
  99. */
  100. PUBLIC BOOL HTAA_passwdMatch ARGS2(CONST char *, password,
  101.                    CONST char *, encrypted)
  102. {
  103.     char *result;
  104.     int len;
  105.     int status;
  106.  
  107.     if (!password || !encrypted ||
  108.     13*((strlen(password)+7)/8) != strlen(encrypted))
  109.     return NO;
  110.  
  111.     len = strlen(encrypted);
  112.  
  113.     if (!(result = (char*)malloc(len + 1)))
  114.     outofmem(__FILE__, "HTAA_encryptPasswd");
  115.  
  116.     *result = (char)0;
  117.     while (len > 0) {
  118.     char salt[3];
  119.     char chunk[9];
  120.     CONST char *cur1 = password;
  121.     CONST char *cur2 = encrypted;
  122.     char *tmp;
  123.  
  124.     salt[0] = *cur2;
  125.     salt[1] = *(cur2+1);
  126.     salt[2] = (char)0;
  127.  
  128.     strncpy(chunk, cur1, 8);
  129.     chunk[8] = (char)0;
  130.  
  131.     tmp = crypt((char*)password, salt);
  132.     strcat(result, tmp);
  133.     free(tmp);
  134.  
  135.     cur1 += 8;
  136.     cur2 += 13;
  137.     len -= 13;
  138.     } /* while */
  139.  
  140.     status = strcmp(result, encrypted);
  141.  
  142.     if (TRACE)
  143.     fprintf(stderr,
  144.         "%s `%s' (encrypted: `%s') with: `%s' => %s\n",
  145.         "HTAA_passwdMatch: Matching password:",
  146.         password, result, encrypted,
  147.         (status==0 ? "OK" : "INCORRECT"));
  148.  
  149.     free(result);
  150.  
  151.     if (status==0)
  152.     return YES;
  153.     else
  154.     return NO;
  155. }
  156.  
  157.  
  158. /* PUBLIC                        HTAAFile_readPasswdRec()
  159. **            READ A RECORD FROM THE PASSWORD FILE
  160. ** ON ENTRY:
  161. **    fp        open password file
  162. **    out_username    buffer to put the read username, must be at
  163. **            least MAX_USERNAME_LEN+1 characters long.
  164. **    out_passwd    buffer to put the read password, must be at
  165. **            least MAX_PASSWORD_LEN+1 characters long.
  166. ** ON EXIT:
  167. **    returns        EOF on end of file,
  168. **            otherwise the number of read fields
  169. **            (i.e. in a correct case returns 2).
  170. **    out_username    contains the null-terminated read username.
  171. **    out_password    contains the null-terminated read password.
  172. **
  173. ** FORMAT OF PASSWORD FILE:
  174. **    username:password:maybe real name or other stuff
  175. **                (may include even colons)
  176. **
  177. **    There may be whitespace (blanks or tabs) in the beginning and
  178. **    the end of each field. They are ignored.
  179. */
  180. PUBLIC int HTAAFile_readPasswdRec ARGS3(FILE *, fp,
  181.                     char *, out_username,
  182.                     char *, out_password)
  183. {
  184.     char terminator;
  185.     
  186.     terminator = HTAAFile_readField(fp, out_username, MAX_USERNAME_LEN);
  187.  
  188.     if (terminator == EOF) {                /* End of file */
  189.     return EOF;
  190.     }
  191.     else if (terminator == CR  ||  terminator == LF) {    /* End of line */
  192.     HTAAFile_nextRec(fp);
  193.     return 1;
  194.     }
  195.     else {
  196.     HTAAFile_readField(fp, out_password, MAX_PASSWORD_LEN);
  197.     HTAAFile_nextRec(fp);
  198.     return 2;
  199.     }
  200. }
  201.  
  202.  
  203.  
  204. /* PUBLIC                        HTAA_checkPassword()
  205. **        CHECK A USERNAME-PASSWORD PAIR
  206. ** ON ENTRY:
  207. **    username    is a null-terminated string containing
  208. **            the client's username.
  209. **    password    is a null-terminated string containing
  210. **            the client's corresponding password.
  211. **    filename    is a null-terminated absolute filename
  212. **            for password file.
  213. **            If NULL or empty, the value of
  214. **            PASSWD_FILE is used.
  215. ** ON EXIT:
  216. **    returns        YES, if the username-password pair was correct.
  217. **            NO, otherwise; also, if open fails.
  218. */
  219. PUBLIC BOOL HTAA_checkPassword ARGS3(CONST char *, username,
  220.                      CONST char *, password,
  221.                      CONST char *, filename)
  222. {
  223.     FILE *fp = NULL;
  224.     char user[MAX_USERNAME_LEN+1];
  225.     char pw[MAX_PASSWORD_LEN+1];
  226.     int status;
  227.     
  228.     if (filename && *filename)  fp = fopen(filename,"r");
  229.     else            fp = fopen(PASSWD_FILE,"r");
  230.  
  231.     if (!fp) {
  232.     if (TRACE) fprintf(stderr, "%s `%s'\n",
  233.                "HTAA_checkPassword: Unable to open password file",
  234.                (filename && *filename ? filename : PASSWD_FILE));
  235.     return NO;
  236.     }
  237.     do {
  238.     if (2 == (status = HTAAFile_readPasswdRec(fp,user,pw))) {
  239.         if (TRACE)
  240.         fprintf(stderr,
  241.             "HTAAFile_validateUser: %s \"%s\" %s \"%s:%s\"\n",
  242.             "Matching username:", username,
  243.             "against passwd record:", user, pw);
  244.         if (username  &&  user  &&  !strcmp(username,user)) {
  245.         /* User's record found */
  246.         if (pw) { /* So password is required for this user */
  247.             if (!password ||
  248.             !HTAA_passwdMatch(password,pw)) /* Check the password */
  249.             status = EOF;    /* If wrong, indicate it with EOF */
  250.         }
  251.         break;  /* exit loop */
  252.         }  /* if username found */
  253.     }  /* if record is ok */
  254.     } while (status != EOF);
  255.  
  256.     fclose(fp);
  257.     
  258.     if (TRACE) fprintf(stderr, "HTAAFile_checkPassword: (%s,%s) %scorrect\n",
  259.                username, password, ((status != EOF) ? "" : "in"));
  260.  
  261.     if (status == EOF)  return NO;  /* We traversed to the end without luck */
  262.     else                return YES; /* The user was found */
  263. }
  264.  
  265.